<?php
class ManageBookingVipForm extends Form{
	function ManageBookingVipForm(){
		Form::Form('ManageBookingVipForm');
		
		CGlobal::$website_title = "Quản trị booking vip";
		
		$this->link_css('style/aznet/ui.datepicker.css');
		$this->link_js('javascript/jquery/ui.datepicker.js');
	}

	function draw(){	
		global $display;
		$this->beginForm();
		
		AZLib::getCats();
		
		$items          = array();
		$item_bookings 	= array();
		
		$where='';
		$order='id DESC';
		
		$catid			=	Url::get('catid',0);
		$item_id		=	Url::get('item_id',0);
		$user_add		=	Url::get('user_add','');
		$num_contract	=	Url::get('num_contract','');
		$status			=	Url::get('status',0);

		
		$time_start = 0;
		$time_end 	= 0;
		
		$date_start = Url::get('date_start');
		$date_end 	= Url::get('date_end');
		
		if($date_start)
		{
			$arr = explode('-',$date_start);
			
			$time_start = mktime(0,0,0,$arr[1],$arr[0],$arr[2]);
		}
		
		if($date_end)
		{
			$arr = explode('-',$date_end);
			
			$time_end = mktime(23,59,59,$arr[1],$arr[0],$arr[2]);
		}
		
		if( $time_start >= $time_end )
		{
			$date_end = '';
			$time_end = 0;
		}
		
		if( $time_start )
		{
			$where.=($where!=''?' AND ':'WHERE ')." time_start >= $time_start";
			
			$order='time_start ASC, id DESC';
		}
		
		if( $time_end )
		{
			$where.=($where!=''?' AND ':'WHERE ')." time_start <= $time_end";
		}
		
		$display ->add('date_start',	$date_start);
		$display ->add('date_end',	$date_end);
		
		$time_start2 = 0;
		$time_end2 	= 0;
		
		$date_start2 = Url::get('date_start2');
		$date_end2 	= Url::get('date_end2');
		
		if($date_start2)
		{
			$arr = explode('-',$date_start2);
			
			$time_start2 = mktime(0,0,0,$arr[1],$arr[0],$arr[2]);
		}
		
		if($date_end2)
		{
			$arr = explode('-',$date_end2);
			
			$time_end2 = mktime(23,59,59,$arr[1],$arr[0],$arr[2]);
		}
		
		if( $time_start2 >= $time_end2 )
		{
			$date_end2 = '';
			$time_end2 = 0;
		}
		
		
		if( $time_start2 )
		{
			$where.=($where!=''?' AND ':'WHERE ')." time_end >= $time_start2";
			
			if($order != 'time_start ASC, id DESC')
			{
				$order = 'time_end ASC, id DESC';
			}
		}
		
		if( $time_end2 )
		{
			$where.=($where!=''?' AND ':'WHERE ')." time_end <= $time_end2";
		}
		
		$display ->add('date_start2',	$date_start2);
		$display ->add('date_end2',	$date_end2);
		
		if($status!=3)
		{
            $where.=($where!=''?' AND ':'WHERE ')." status = $status";
		}
        elseif($status == 3){
            $where.=($where!=''?' AND ':'WHERE ')." status in (0,1)";
        }
		
		if($item_id)
		{
			$where.=($where!=''?' AND ':'WHERE ')." item_id = $item_id";
		}
		if($user_add!='')
		{
			$where.=($where!=''?' AND ':'WHERE ')." user_add = '$user_add'";
		}
        
		if($num_contract!='')
		{
			$where.=($where!=''?' AND ':'WHERE ')." num_contract = '$num_contract'";
		}
		
		$zone_id = Url::get("zone_id",0);
		
		$arr = array(
					0 => "-- Tất cả Zone --",
					);

					
		foreach (CGlobal::$allZones as $zone)
		{
			$arr[$zone['id']] = $zone['name'];
		}
		
		$display->add('zone_option',AZLib::getOption($arr,$zone_id));
		
		$arr = array(
					0 => "-- Tất cả Danh mục --",
					);
					
					
		if(isset(CGlobal::$allCategories[$catid]))
		{
			if(CGlobal::$allCategories[$catid]['parent_id'])
			{
				$where.=($where!=''?' AND ':'WHERE ')." cat_id = $catid";
			}
			else 
			{
				if(isset(CGlobal::$subCategories[$catid]) && CGlobal::$subCategories[$catid])
				{
					$where.=($where!=''?' AND ':'WHERE ')." (top_cat = $catid OR cat_id IN(".implode(",",array_keys(CGlobal::$subCategories[$catid]))."))";
				}
				else
				{
					$where.=($where!=''?' AND ':'WHERE ')." top_cat = $catid";
				}
			}
		}
		
		if(CGlobal::$allCategories && CGlobal::$subCategories)
		{
			foreach (CGlobal::$subCategories as $topid => $subcats)
			{
				if(!$zone_id || ($zone_id && isset(CGlobal::$allCategories[$topid]) && in_array($zone_id,CGlobal::$allCategories[$topid]['zones'])))
				{
					if(isset(CGlobal::$allCategories[$topid]))
					{
						$arr[$topid] = CGlobal::$allCategories[$topid]['name']." ($topid)";
					}
					else 
					{
						$arr[$topid] = "($topid)";
					}
					
					foreach ($subcats as $cid=>$cat)
					{
						$arr[$cid] = "--------{$cat['name']} ($cid)";
					}
				}
			}
		}
		
		$display->add('cat_option',AZLib::getOption($arr,$catid));
		
		
		
		$display->add('item_id',$item_id);
		$display->add('status',$status);
		$display->add('user_add',$user_add);
		$display->add('num_contract',$num_contract);
		
		$re = DB::query("SELECT * FROM bookings_vip $where ORDER BY $order");
		
		$item_ids = '';
		
		if($re)
		{
			while($v_item = mysql_fetch_assoc($re))
			{
				$zid 		= 0;
				$zone_name	= 0;
				
				$topcid 	= 0;
				
				$cid 		= 0;
				
				if($v_item['cat_id'])
				{
					$cid = $v_item['cat_id'];
					
					if(isset(CGlobal::$allCategories[$cid]))
					{
						$topcid 	= CGlobal::$allCategories[$cid]['parent_id'];
					}
				}
				elseif($v_item['top_cat'])
				{
					$topcid	= $v_item['top_cat'];
				}
				
				if(!$topcid && $cid)
				{
					if(isset(CGlobal::$allCategories[$cid]))
					{
						$topcid = CGlobal::$allCategories[$cid]['parent_id'];
					}
				}
				
				if($topcid)
				{
					if(isset(CGlobal::$allCategories[$topcid]))
					{
						foreach (CGlobal::$allCategories[$topcid]['zones'] as $zoneid)
						{
							if($zoneid)
							{
								$zid = $zoneid; break;
							}
						}
					}
				}
				
				$zone_name 	= "[$zid] ";
				
				if($zid && isset(CGlobal::$allZones[$zid]))
				{
					$zone_name 	.= CGlobal::$allZones[$zid]['name'];
				}
				
				if(!$zone_id || ($zone_id && $zid == $zone_id))
				{
					$v_item['zone'] = $zone_name;
					$v_item['time'] = "<b>Tạo:</b> ".date("d/m/Y H:i",$v_item['time_add'])." bởi <b>".$v_item['user_add']."</b>";
					
					if($v_item['time_add'] != $v_item['time_edit']){
						$v_item['time'] .= "<br /><b>Sửa:</b> ".date("d/m/Y H:i",$v_item['time_edit'])." bởi <b>".$v_item['user_edit']."</b>";
					}

                    if($v_item['user_update_status'] != '' && $v_item['time_update_status'] != 0){
                        $v_item['time'] .= "<br /><b>Update:</b> ".date("d/m/Y H:i",$v_item['time_update_status'])." bởi <b>".$v_item['user_update_status']."</b>";
                    }

                    switch ($v_item['status']){
                        case -1:
                            $v_item['str_status'] = "<font color=red><b>Đã xóa <br/>{$v_item['user_del']}</b></font>";
                            break;
                        case 1: 
                            $v_item['str_status'] = "<font color=black><b>Đã xử lý</b></font>";
                            break;
                        default:
                            $v_item['str_status'] = "<font color=green><b>Chưa xử lý</b></font>";

                            $v_item['permis'] = 0;
                            if(User::have_permit(ADMIN_VIP_ITEM)){
                                $v_item['permis'] 			= 1;
                                $v_item['update_status'] 	= Url::build_current(array('cmd'=>'update_status','id'=>$v_item['id']));
                                $v_item['active'] 			= Url::build_current(array('cmd'=>'active','id'=>$v_item['id']));
                            }
                            
                            $v_item['edit'] 	= Url::build_current(array('cmd'=>'edit','id'=>$v_item['id']));
                            $v_item['delete']   = Url::build_current(array('cmd'=>'delete','id'=>$v_item['id']));
                            break;
                    }

					if($v_item['time_start'] > TIME_NOW)
					{
						$v_item['time_start'] = '<font color="#c00"><b>'.date("d/m/Y",$v_item['time_start']).'</b></font>';
					}
					else
					{
						$v_item['time_start'] = date("d/m/Y",$v_item['time_start']);
					}
					
					if($v_item['time_end'])
					{
						if($v_item['time_end'] < TIME_NOW){
							$v_item['time_end'] = '<font color="#f00"><b>'.date("d/m/Y",$v_item['time_end']).'</b></font>';
						}
						else{
							$v_item['time_end'] = date("d/m/Y",$v_item['time_end']);
						}
					}
					else
					{
						$v_item['time_end'] = '';
					}
					
					$item_ids.=($item_ids!=''?',':'').$v_item['item_id'];

                    //check vi tri vip
                ($v_item['top_cat'] != 0) ? $cat_id=$v_item['top_cat'] : $cat_id = $v_item['cat_id'];

                $item_vip = DB::select_all('item_vip', "(catid = $cat_id OR level_1_catid = $cat_id) AND status = 1 AND time_end >".TIME_NOW);

                $i = 0;
                $sa = $v_item['time_start'];
                $ea = $v_item['time_end'];

                foreach($item_vip as $item){
                    if($sa > $item['time_start'] && $sa < $item['time_end']){
                        $i++;
                    }elseif($ea > $item['time_start'] && $ea < $item['time_end']){
                        $i++;
                    }elseif($sa < $item['time_start'] && $ea > $item['time_end']){
                        $i++;
                    }
                }
                $v_item['over_slot'] = 0;
                if($i >= 2){
                    $v_item['over_slot'] = 1;
                }
                //end check vi tri

                //check trung ID
                $v_item['exist_id'] = 0;
                foreach($item_vip as $item){
                    if($v_item['item_id'] == $item['item_id']){
                        $v_item['exist_id'] = 1;
                    }
                }
                //end check trung ID
					$item_bookings[$v_item['id']] = $v_item;
				}
			}
		}
		
		
		if($item_ids!='')
		{
			$re = DB::query("SELECT * FROM item WHERE id IN($item_ids)");
			if($re){
				while($item = mysql_fetch_assoc($re)){
					$item['item_link'] = Url::build('item_detail',array('id'=>$item['id'],'ebname'=>AZLib::safe_title($item['name'])));
					
					$items[$item['id']] = $item;
				}
			}
		}
		
		foreach ($item_bookings as &$v_item){
			if(isset($items[$v_item['item_id']])){
				$v_item['item_link'] = $items[$v_item['item_id']]['item_link'];
				$v_item['item_name'] = stripslashes($items[$v_item['item_id']]['name']);
				
				if($items[$v_item['item_id']]['status'] == -1){
					$v_item['item_name'] .= ' <font color="#c00">Tin xoá</font>';
				}
				elseif($items[$v_item['item_id']]['status'] != 1){
					$v_item['item_name'] .= ' <font color="#c00">KD</font>';
				}
				
				$top_catid = $items[$v_item['item_id']]['level_1_catid'];
				$catid 	   = $items[$v_item['item_id']]['category_id'];
				
				
				
				$v_item['top_cat_name'] = ($v_item['top_cat']) ? CGlobal::$allCategories[$v_item['top_cat']]['name'] : '';
				$v_item['cat_name'] 	= ($v_item['cat_id']) ? CGlobal::$allCategories[$v_item['cat_id']]['name'] : '';
				
				if ($v_item['cat_id'] && $v_item['cat_id'] != $catid)
				{
					$v_item['wrong_cat'] = '<font color="red">(sai chủng loại)</font>';
				}
				
				$v_item['item_cat_name'] = "<font color=blue>".CGlobal::$allCategories[$top_catid]['name'].'</font> /<br /><font color=green>'.CGlobal::$allCategories[$catid]['name'].'</font>';
				
				if(isset(CGlobal::$allCategories[$v_item['cat_id']])){
					$v_item['cat_vip'] = CGlobal::$allCategories[$v_item['cat_id']]['name'];
				}
				else{
					$v_item['cat_vip'] = '';
				}
			}
			else
			{
				$v_item['item_name'] = 'Tin không tồn tại';
				$v_item['cat_name']  = '';
			}
		}
		
		$display->add('items',$item_bookings);
		
		$display->output('ManageBookingVip');
		$this->endForm();
	}
}
?>